//## 통계 그리드 표시 ####################
var G_curPage01 = 1;

function grid01List(page,accSum)
{	
	var gridNo = 1;
	var xmlNode = "Stat";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage01;
	if (page == undefined) page = 1;
	else G_curPage01 = page;
	var limit = 50;

	var valUrl = "/action/accountStat/list.asp";

	var Parm = "stYear="+$("#findYear").val();
	 		Parm += " &findInOut="+$("#findInOut").val();	 		
		 //Parm += " &compCd="+$("#findCompCd").val();
	
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = "left,right,right,right,right,right,right,right,right,right,right,right,right,right,right".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = "N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			if (ttlCnt%limit == 0) var totalPage = parseInt((ttlCnt / limit),0);
			else 	var totalPage = parseInt((ttlCnt / limit),0)  + 1;
			var cPageNo = (totalPage) * limit;
			if (cPageNo > ttlCnt) cPageNo = ttlCnt;
			
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var ttlAmt = 0;
			var ttlIncAmt = 0;
			var ttlOrgAmt = 0;
			var oldSeq = "";
			var empty = "&nbsp;";
			var arrTotal = new Array();
			$(xml).find(xmlNode).each(function(idx){

				no = cPageNo;
				if (k>startNo && k<=endNo)
				{
					var company			= $(this).find("company").text();
					var m01					= $(this).find("m01").text();
					var m02					= $(this).find("m02").text();
					var m03					= $(this).find("m03").text();
					var m04					= $(this).find("m04").text();
					var m05					= $(this).find("m05").text();
					var m06					= $(this).find("m06").text();
					var m07					= $(this).find("m07").text();
					var m08					= $(this).find("m08").text();
					var m09					= $(this).find("m09").text();
					var m10					= $(this).find("m10").text();
					var m11					= $(this).find("m11").text();
					var m12					= $(this).find("m12").text();
					var total					= $(this).find("total").text();
					var ttlRate					= $(this).find("ttlRate").text();
					var arrStyle = new Array();

					
					if (company == "Y^생산1팀") 
					{
						bgColor="#f4fdff";
						arrStyle[0] = "font-weight:bold;text-align:center;";
						company = "생산1팀";
						for (k=1; k<=13; k++) arrStyle[k] = "color:#3333ff;";
						ttlRate = "";
					}
					if (company == "Y^생산2팀") 
					{
						bgColor="#f4fdff";
						arrStyle[0] = "font-weight:bold;text-align:center;";
						company = "생산2팀";
						for (k=1; k<=13; k++) arrStyle[k] = "color:#3333ff;";
						ttlRate = "";
					}
					
					if (company == "Y^총액") 
					{
						bgColor="#f0faff";
						arrStyle[0] = "font-weight:bold;text-align:center;";
						if ($("#findInOut").val() == "IN") 	company = "수입합계";
						else 	company = "지출합계";
						for (k=1; k<=13; k++) arrStyle[k] = "color:#3333ff;";
						ttlRate = "";
					}
					
					if (company == "Y^총액") 
					{
						bgColor="#f0faff";
						arrStyle[0] = "font-weight:bold;text-align:center;";
						if ($("#findInOut").val() == "IN") 	company = "수입합계";
						else 	company = "지출합계";
						for (k=1; k<=13; k++) arrStyle[k] = "color:#3333ff;";
						ttlRate = "";
					}
					if (company == "Z^지출총액") 
					{
						arrStyle[0] = "font-weight:bold;text-align:center;";
						company = "총 지출";
						for (k=1; k<=13; k++) arrStyle[k] = "color:#ff0000;";
						ttlRate = "";
					}
					if (company == "Z^수입총액") 
					{
						arrStyle[0] = "font-weight:bold;text-align:center;";
						company = "총 수입";
						for (k=1; k<=13; k++) arrStyle[k] = "color:#ff0000;";
						ttlRate = "";
					}
					if (company == "Z^총수익") 
					{
						arrStyle[0] = "font-weight:bold;text-align:center;";
						company = "수익 총액";
						for (k=1; k<=12; k++) 
						{
							arrStyle[k] = "color:#3333ff;";
							if (k < 10) var m = "m0";
							else var m = "m";
							if (eval(m+k).split(",").join("") < 0) arrStyle[k] = "color:#ff0000;";
						}
						if (k==13 && total.split(",").join("")<0) arrStyle[k] = "color:#ff0000;";			
						ttlRate = "";			
					}
					var arrVal = new Array();
					arrVal[0] = "<p style='"+arrStyle[0]+"'>"+company+"</p>";
					arrVal[1] = "<p style='"+arrStyle[1]+"'>"+m01+"</p>";
					arrVal[2] = "<p style='"+arrStyle[2]+"'>"+m02+"</p>";
					arrVal[3] = "<p style='"+arrStyle[3]+"'>"+m03+"</p>";
					arrVal[4] = "<p style='"+arrStyle[4]+"'>"+m04+"</p>";
					arrVal[5] = "<p style='"+arrStyle[5]+"'>"+m05+"</p>";
					arrVal[6] = "<p style='"+arrStyle[6]+"'>"+m06+"</p>";
					arrVal[7] = "<p style='"+arrStyle[7]+"'>"+m07+"</p>";
					arrVal[8] = "<p style='"+arrStyle[8]+"'>"+m08+"</p>";
					arrVal[9] = "<p style='"+arrStyle[9]+"'>"+m09+"</p>";
					arrVal[10] = "<p style='"+arrStyle[10]+"'>"+m10+"</p>";
					arrVal[11] = "<p style='"+arrStyle[11]+"'>"+m11+"</p>";
					arrVal[12] = "<p style='"+arrStyle[12]+"'>"+m12+"</p>";
					arrVal[13] = "<p style='"+arrStyle[13]+"'>"+total+"</p>";
					arrVal[14] = "<p style='"+arrStyle[14]+"'>"+ttlRate+"</p>";

					if (company == "수입합계" || company == "지출합계" ) 
					{
						arrTotal = arrVal;
					}
					var link = "";
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				cPageNo--;
				k++;			
			});
			bgColor="#f0faff";
			gridList = fnDrawCell(arrTotal, bgColor, cellWidth, cellAlign, '', '', cellLink);
			$("#"+"gridBody"+gridNo).prepend(gridList);
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});
}

//## 회사별 합계 검색
function fnCompList(company, compCd)
{
	fnSearchReset();
	fnFindCompany(company,compCd,'findCompCd');
	setTimeout("grid01List(1,'Company');",500);
}

//## 계정별 합계 검색
function fnAccList(accountCd)
{
	fnSearchReset();
	$("#findAccountCd").val(accountCd);
	grid01List(1,'Account');
}


function fnSetSame()
{
	$("#incAmount").val($("#amount").val());
}

//## 지급일 동기화
function fnSetSameDt()
{
	$("#bankDt").val($("#incomeDt").val());
	$("#actionDt").val($("#incomeDt").val());
}

//## 회계(입금) Reset 기능
function fnReset()
{
	$("#income").val("");
	$("#memo").val("");
	$("#billMemo").val("");
	$("#billCd").val("");
	$("#accountCd").val("");
	$("#amount").val("");
	$("#orgAmount").val("");
	$("#tax").val("");
	$("#part").val("");
	$("#compCd").val("");
	$("#company").val("");
	$("#seq").val("");
	$("#flag").val("Y");
	$("#incAmount").val("");
	$("#incSeq").val("");
	$("#bankDt").val("");
	$("#actionDt").val("");
	$("#incFlag").val("Y");
}

function fnResetTerms(f)
{
	if (f=="t") 
	{
		$("#stDate").val("");
		$("#endDate").val("");		
	}
	else $("#findMnth").val("");		
}

function fnIncResultReset()
{
	$("#incSeq").val("");
	$("#incAmount").val("");
	$("#incMemo").val("");
	$("#incFlag").val("Y");
}

function fnSetTax()
{
	var amount = $("#amount").val();
	amount = amount.split(",").join("");
	if (amount > 0)
	{
		var tax = 0;
		tax = Math.round(parseInt(amount,10)/11);

		var orgAmount = parseInt(amount,10) - parseInt(tax,10);
		orgAmount = getNumWithComma(orgAmount);
		tax = getNumWithComma(tax);

		$("#tax").val(tax);
		$("#orgAmount").val(orgAmount);
	}
}

//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findIncome").val("");
	$("#findAccountCd").val("");
	$("#findComapny").val("");
	$("#findCompCd").val("");
	$("#findPart").val("");
}